//
//  DDInvoiceInfoEditVC.swift
//  DingDongZP
//
//  Created by 三秒文化 on 2016/10/18.
//  Copyright © 2016年 51ledian.com. All rights reserved.
//

import UIKit

enum DDInvoiceSettingInfoVCType {
    
    case Select //选中后动作 回调 or pop
    case Edit //select 后编辑
}

class DDInvoiceSettingInfoVC: LDBaseTableViewVC{

    // MARK: - Table view data source
    private let cellDetailID = "DDChoiceInvoiceInfoVCDetailID"
    
    var model:DDInvoiceInfoModel? {
        
        didSet{
            
            if model?.list.count == 0 {
                labTxt.hidden = true
            } else {
                labTxt.hidden = false
            }
        }
    }
    var selListModel:DDInvoiceInfoModelList?
    private var type = DDInvoiceSettingInfoVCType.Edit
    private var selInvoiceID:String?
    private var labTxt:UILabel = {
    
        let lab = UILabel.setDetailsLab("  向左滑动删除", fontSize: 14, titleColor: LDFontColor.DeepGray, Alignment: .Left)
        lab.frame = CGRectMake(20, 1, 100, 30)
        lab.hidden = true
        
        return lab
    }()
    
    override func viewDidLoad() {
        
        super.viewDidLoad()
        initContent()
        
        
        tableView.tableFooterView = labTxt
        tableView.contentInset = UIEdgeInsetsMake(LDFrame.TableCellMargin, 0, 0, 0)
        tableView.separatorStyle = .None
        
        
    }
    
    func initContent() {
        
//        RACObserve(self, "model").subscribeNext { (obj) in
//            
//        }
    }
    
    override func viewWillAppear(animated: Bool) {
        
        requsetNet()
    }
    
    //请求列表
    func requsetNet() {
        
        LDUserInfoNet.sharedInstance.getInvoiceBillInfoList({[weak self] (dictArr) in
            
            self?.model = DDInvoiceInfoModel.yy_modelWithDictionary(dictArr)
            self?.tableView.reloadData()
            
//            if self?.model?.list.count == 0 {
//                
//                self?.labTxt.hidden = true
//            } else {
//                self?.labTxt.hidden = false
//            }
            
        }) { (err) in
            
            
        }
    }
    //删除某个列表
    func deleteInvoiceInfo(id:String,index:NSIndexPath) {
        
        LDUserInfoNet.sharedInstance.deleteInvoiceBillList(id, succ: { [weak self] (dictArr) in
            
            LDShowHUB.shareInstance.showSucc("删除成功")
            self?.model?.list.removeAtIndex(index.section)
            self?.tableView.deleteSections(NSIndexSet(index: index.section), withRowAnimation: .Fade)
            
        }) {(err) in
            
            LDShowHUB.shareInstance.showErr("删除失败")
        }
    }
    
   override func setNav() {
        
        if type == .Edit {
            
            let rightBar = UIBarButtonItem.init(image: UIImage(named:"add+"), style: UIBarButtonItemStyle.Done, target: self, action: #selector(tapRightBar))
            navigationItem.rightBarButtonItem = rightBar
            
        } else {
            
        }
        
    }
    
    //增加发票信息
     override func tapRightBar() {
        
        let vc = DDInvoiceAddInfoEditVC()
        vc.title = "增加发票信息"
        navigationController?.pushViewController(vc, animated: true)
    }
    
    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        
        let vc = DDInvoiceAddInfoEditVC()
        
        selListModel = model?.list[indexPath.section] ?? DDInvoiceInfoModelList()
        vc.preModel = selListModel
        
        vc.title = "修改发票信息"
        navigationController?.pushViewController(vc, animated: true)
    }
    
}

extension DDInvoiceSettingInfoVC {
    

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        
        return model?.list.count ?? 0
    }
    
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return 1//model?.list.count ?? 0
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
        
        var cell = tableView.dequeueReusableCellWithIdentifier(cellDetailID) as? DDInvoiceDetailViewCell
        if cell == nil {
            
            cell = DDInvoiceDetailViewCell(topMargin: nil,style: .Default, reuseIdentifier: cellDetailID)
        }
        
        let listModel = model?.list[indexPath.section] //as? DDInvoiceInfoModelList
        let placeStr = String.init(format: "%@-%@-%@", listModel!.province,listModel!.city,listModel?.dist ?? "")
        cell?.descLabArr = [listModel!.name,listModel!.link_name,listModel!.link_mobile,placeStr,listModel!.address]
        cell?.detailsBar?.selStatusLab!.hidden = listModel!.state == 1 ? false:true
        
        return cell!
        
    }
    
    override func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        
        return LDFrame.TableCellMargin
    }
    
    override func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        
        let view = UIView()
        view.backgroundColor = LDGolbalColor.BgGoundGray
        return UIView()
        
    }
    
    override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        
        return 270
    }
}

//滑动操作
extension DDInvoiceSettingInfoVC {
    
    override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
        
        return true
    }
    
    override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
        
        if editingStyle == UITableViewCellEditingStyle.Delete {
            
            
            selListModel = model?.list[indexPath.section] //as? DDInvoiceInfoModelList ?? DDInvoiceInfoModelList()
            
            if selListModel?.id != nil {
                
                deleteInvoiceInfo(selListModel?.id!.stringValue ?? "",index: indexPath)
                
            }
            tableView.endEditing(true)
            
        }
    }
    
    override func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle {
        
        return .Delete
    }
    
    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        
        tableView.setEditing(false, animated: true)
    }
}

